
package com.viweb.storemanager.core;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.MutableComboBoxModel;


public class ObjectComboBoxModel extends AbstractListModel
        implements MutableComboBoxModel, Serializable {

    private List comboboxData;
    private Object selectedObject;

    /** Creates a new instance of DefaultObjectComboBoxModel */
    public ObjectComboBoxModel() {
        this.comboboxData = new ArrayList();

        if (this.getSize() > 0) {
            this.selectedObject = this.getElementAt(0);
        }
    }

    public ObjectComboBoxModel(List comboboxData) {
        if (comboboxData == null) {
            this.comboboxData = new ArrayList();
        } else {
            this.comboboxData = comboboxData;
        }

        if (this.getSize() > 0) {
            this.selectedObject = this.getElementAt(0);
        }
    }

    public int getSize() {
        return this.comboboxData.size();
    }

    public Object getElementAt(int index) {
        if (index >= 0 && index < this.comboboxData.size()) {
            return this.comboboxData.get(index);
        } else {
            return null;
        }
    }

    public void addElement(Object obj) {
        this.comboboxData.add(obj);
        fireIntervalAdded(this, this.comboboxData.size() - 1, this.comboboxData.size() - 1);
        if (this.comboboxData.size() == 1 && this.selectedObject == null && obj != null) {
            this.setSelectedItem(obj);
        }
    }

    public void removeElement(Object obj) {
        int index = this.comboboxData.indexOf(obj);
        if (index != -1) {
            this.removeElementAt(index);
        }
    }

    public void insertElementAt(Object obj, int index) {
        this.comboboxData.add(index, obj);
        fireIntervalAdded(this, index, index);
    }

    public void removeElementAt(int index) {
        if (this.getElementAt(index) == this.selectedObject) {
            if (index == 0) {
                this.setSelectedItem(this.getSize() == 1 ? null : this.getElementAt(index + 1));
            } else {
                this.setSelectedItem(this.getElementAt(index - 1));
            }
        }

        this.comboboxData.remove(index);

        fireIntervalRemoved(this, index, index);
    }

    public void setSelectedItem(Object anItem) {
        if ((this.selectedObject != null && !this.selectedObject.equals(anItem))
                || this.selectedObject == null && anItem != null) {
            this.selectedObject = anItem;
            fireContentsChanged(this, -1, -1);
        }
    }

    public int getIndexOf(Object obj) {
        return this.comboboxData.indexOf(obj);
    }

    public void setObjects(List objects) {
        this.comboboxData = objects;
    }

    public Object getSelectedItem() {
        return this.selectedObject;
    }

    public void updateModel(List comboboxData) {
        if (comboboxData == null) {
            this.comboboxData = new ArrayList();
        } else {
            this.comboboxData = comboboxData;
        }
    }
}
